const path = require('path');
const { defineConfig } = require('rollup');
const json = require('@rollup/plugin-json');
const terser = require('@rollup/plugin-terser');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('@rollup/plugin-typescript');
const nodeResolve = require('@rollup/plugin-node-resolve');

const typescriptP = typescript({
	compilerOptions: {
		target: 'ESNext',
		module: 'ESNext',
		strict: true,
		moduleResolution: 'node',
		allowImportingTsExtensions: false,
	},
});

module.exports = defineConfig([
	// 打包主进程文件
	{
		input: path.join(__dirname, './src/main/main.ts'), // 入口文件路径
		output: {
			file: path.join(__dirname, './dist/bundle.js'), // 输出文件路径
			format: 'cjs', // 输出格式为CommonJS模块
		},
		plugins: [nodeResolve(), commonjs(), json(), terser(), typescriptP],
		external: ['electron'],
		onwarn(warning, warn) {
			// 禁用 "Use of eval is strongly discouraged" 和 "Circular dependencies" 警告
			if (warning.code === 'EVAL' || warning.code === 'CIRCULAR_DEPENDENCY') return;

			// 正常显示其他警告
			warn(warning);
		},
	},
	// 打包预加载文件
	{
		input: path.join(__dirname, './src/main/utils/preload.ts'), // 入口文件路径
		output: {
			file: path.join(__dirname, './dist/preload.js'), // 输出文件路径
			format: 'cjs', // 输出格式为CommonJS模块
		},
		plugins: [terser(), typescriptP],
		external: ['electron'],
	},
]);
